<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">

		<title>TRANS-INFO</title>
		<meta name="keywords" content="TRANS-INFO">
		<meta name="description" content="TRANS-INFO">

		<link href="../css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
		<link href="../css/font-awesome.css?v=4.4.0" rel="stylesheet">
		<link href="../css/animate.css" rel="stylesheet">
		<link href="../css/style.css?v=4.1.0" rel="stylesheet">

		<!-- treegrid -->
		<link href="../css/plugins/treeview/bootstrap-treeview.css" rel="stylesheet">

	</head>

	<body class="gray-bg">
		<div class="wrapper wrapper-content animated fadeInRight">

			<div class="row">
				<div class="col-sm-12">
					<div class="ibox float-e-margins">
						<div class="ibox-title">
							<h5>
							<i class="fa fa-table"></i> 调用关系拓扑图
						</h5>
							<div class="ibox-tools">
								<a class="collapse-link"> <i class="fa fa-chevron-up"></i>
								</a>
								<a class="dropdown-toggle" data-toggle="dropdown" href="rpc_list.html#"> <i class="fa fa-wrench"></i>
								</a>
								<ul class="dropdown-menu dropdown-user">
									<li>
										<a href="rpc_list.html#">选项1</a>
									</li>
									<li>
										<a href="rpc_list.html#">选项2</a>
									</li>
								</ul>

							</div>
						</div>
						<div class="ibox-content" style="height: 320px;">
							<div class="echarts" id="rpc_link_chart" style="height: 300px;"></div>
						</div>
					</div>
				</div>
			</div>

			<div class="row">
				<div class="col-sm-12">
					<div class="ibox float-e-margins">
						<div class="ibox-title">
							<h5>
							<i class="fa fa-table"></i> 服务级调用链
						</h5>
							<div class="ibox-tools">
								<a class="collapse-link"> <i class="fa fa-chevron-up"></i>
								</a>
								<a class="dropdown-toggle" data-toggle="dropdown" href="trans_info.html"> <i class="fa fa-wrench"></i>
								</a>
								<ul class="dropdown-menu dropdown-user">
									<li>
										<a href="trans_info.html">选项1</a>
									</li>
									<li>
										<a href="trans_info.html">选项2</a>
									</li>
								</ul>

							</div>
						</div>
						<div class="ibox-content">
							<div id="trans_detail" class="test"></div>
							<div><a id="method_info" ><button>查看方法级调用链明细</button></a></div>
						</div>
					</div>
				</div>
			</div>

			

		</div>
		</div>

		<!-- 全局js -->
		<script src="../js/jquery.min.js?v=2.1.4"></script>
		<script src="../js/bootstrap.min.js?v=3.3.6"></script>

		<!-- 自定义js -->
		<script src="../js/content.js?v=1.0.0"></script>
		<script src="../js/mine/format.js"></script>

		<!-- 时间框 -->
		<script src="../js/mine/time.js"></script>

		<!-- treeview js -->
		<script src="../js/plugins/treeview/bootstrap-treeview1.js"></script>
		<script src="../js/plugins/echarts/echarts-all.js"></script>
		<script src="../js/jquery.i18n.properties-min-1.0.9.js"></script>

		<!-- Page-Level Scripts -->
		<script>
		
		
			var traceIdTemp = escape(GetEscapeStr('traceId'));
			
//			var preUrl = "http://127.0.0.1:8041";
//			var preUrl = "http://10.145.196.107:8041";
			var preUrl = getUrl();
			
			function  getUrl(){  
				var url = "";
			    jQuery.i18n.properties({// 加载properties文件  
			    name:'ipAndPort', // properties文件名称  
			    path:'../properties/', // properties文件路径  
			    mode:'map', // 用 Map 的方式使用资源文件中的值  
			    callback: function() {// 加载成功后设置显示内容  
//			       //其中isp_index为properties文件中需要查找到的数据的key值  
			    	url = $.i18n.prop("url");
			    	
			    	
			    }
			    });
			    
			   
			    return url;
			} 

			var centers=[];

			var paramItem = {
				"name": name,
				"password": "d033e22ae348aeb5660fc2140aec35850c4da997",
				"title": "日志分析",
				"url": 'http://10.145.196.83:8084/logv1/index?platform=msc&startTime=2018-01-18&endTime=2018-05-18 12:12:12&searchIndex="' + traceIdTemp + '" AND ' + GetEscapeStr("agentId") + ' AND ' + GetEscapeStr("spanId") + '&selctedLogType=all,error,error,mid,error,mid,counter,timer,gauge'
			};
			window.onload = function() {
				
				var str = GetEscapeStr('traceId');
				var arr = str.split("^");
				var traceId = "";
				$.each(arr, function(i, a) {
					if(i == arr.length - 1){
						traceId = traceId + a;
					}else{
						traceId = traceId + a + "%5E";
					}
					
				});
				
				
				
				$("#method_info").attr("href","method_info.html?traceId=" + traceId + "&agentId=" + GetEscapeStr('agentId') + "&focusTimestamp="+ GetEscapeStr('focusTimestamp') +"&spanId=" + GetEscapeStr('spanId') + "&mRpc=" + GetEscapeStr('mRpc'));
//				$("#method_info").attr("href","http://www.baidu.com");
				
			}
			if(traceIdTemp != null && GetEscapeStr("agentId") != null && GetEscapeStr("spanId") != null) {

				$("#ifr").attr('src', 'http://10.145.196.83:8084/logv1/index?platform=msc&startTime=2018-01-18&endTime=2018-05-18 12:12:12&searchIndex="' + traceIdTemp + '" AND ' + GetEscapeStr("agentId") + ' AND ' + GetEscapeStr("spanId") + '&selctedLogType=all,error,error,mid,error,mid,counter,timer,gauge');
			}

			function getImg(code,applicationName) {
				if(code == "1210") {
					var showCenter;
					var timestamp = 0;
					$.each(centers, function(j, center) {
								if(applicationName == center.applicationType.applicationName) {
									if(center.timeStamp > timestamp){
										showCenter = center.center;
										timestamp = center.timeStamp;
									}
									
								}

							});
					
					if(showCenter = "ordercenter"){
						return 'image://../image/orderCenter.png';
					}else if(showCenter = "cpccenter"){
						return 'image://../image/cpcCenter.png';
					}else if(showCenter = "custcenter"){
						return 'image://../image/customCenter.png';
					}else if(showCenter = "unknown"){
						return 'image://../image/other.png';
					}else if(showCenter = "msc"){
						return 'image://../image/orderCenter.png';
					}
					
				} else if(code == "2") {
					return 'image://../image/USER2.png';
				} else if(code == "2101") {
					return 'image://../image/MYSQL2.png';
				} else if(code == "8200") {
					return 'image://../image/REDIS2.png';
				} else {
					return 'image://../image/UNKNOWN_GROUP2.png';
				}
			}
			$(document).ready(function() {
				
				$.get(preUrl + "/getAllCenterInfos", {},
					//				$.get("http://localhost:8041/applications", {},
					function(data) {
						
//						console.log(data);
						if(data.length != 0){
							centers = data;
						}
//						console.log(JSON.stringify(centers));
					});
				
				
					//获取散点图
								$.get(preUrl + "/transactionInfoDetail", {
//				$.get("http://localhost:8041/transactionInfoDetail", {
						traceId: GetEscapeStr("traceId"),
						focusTimestamp: GetEscapeStr("focusTimestamp"),
						agentId: GetEscapeStr("agentId"),
						spanId: GetEscapeStr("spanId"),
						mRpc: GetEscapeStr("mRpc")
					},
					function(rs) {
						
//						console.log(rs);


						var data = rs;
//						console.log(data.rsList);

						var tree = JSON.stringify(data.rsList);
//						console.log(tree);
						$('#trans_detail').treeview({
							levels: 99,
							ItemHeight: 30,
							expandIcon: 'glyphicon glyphicon-chevron-right',
							collapseIcon: 'glyphicon glyphicon-chevron-down',
							nodeIcon: null,
							showTags: true,
							showBorder: true,
							data: tree
						});

						var nodeArray = [];
						var linkArray = [];
						var temp1 = {
							symbol: 'image://http://www.damndigital.com/wp-content/uploads/2010/12/steve-jobs.jpg',
							symbolSize: [60, 35],
							draggable: true,
							itemStyle: {
								normal: {
									label: {
										position: 'right',
										textStyle: {
											color: 'black'
										}
									}
								}
							}
						};
						nodeArray.push(temp1);
						var nodeList = {};
						var linkList = {};
						
						nodeList = data.nodeDataArray;
						linkList = data.linkDataArray;
						var nodeMap = {};
						var totalRTS = {};
//						alert(nodeList);
					/*	alert(nodeList[0]);
						alert(linkList.length);
*/
						$.each(nodeList, function(i, node) {
							var RTSCount = 0;
							$.each(linkList, function(j, link) {
								if(node.key == link.from) {
									RTSCount = RTSCount + link.totalCount;
								}

							});
							totalRTS[node.applicationName] = RTSCount;

						});

						$.each(linkList, function(j, link) {
							$.each(nodeList, function(i, node) {

								if(node.key == link.from) {
									if(!(node.isWas)) {
										if(node.serviceTypeCode == 2 || node.serviceTypeCode == "2") {
											var sourceV = {
												category: 1,
												symbol: getImg(node.serviceTypeCode,node.applicationName),
												name: node.key,
												initial: [200, 0],
												fixX: true,
												label: node.applicationName,
												value: 1
											};
										} else {
											var sourceV = {
												category: 1,
												symbol: getImg(node.serviceTypeCode,node.applicationName),
												name: node.key,
												initial: [1200, 0],
												fixX: true,
												label: node.applicationName,
												value: 1
											};
										}

									} else {
										var sourceV = {
											category: 1,
											symbol: getImg(node.serviceTypeCode,node.applicationName),
											name: node.key,
											label: node.applicationName ,
											value: 1
										};

									}

									nodeMap[node.key] = sourceV;
									sourceK = node.key;
								} else if(node.key == link.to) {
									if(!(node.isWas)) {
										if(node.serviceTypeCode == 2 || node.serviceTypeCode == "2") {
											var targetV = {
												category: 1,
												symbol: getImg(node.serviceTypeCode,node.applicationName),
												name: node.key,
												label: node.applicationName + '\n RTS:' + link.totalCount,
												value: 1
											};
										} else {
											var targetV = {
												category: 1,
												symbol: getImg(node.serviceTypeCode,node.applicationName),
												name: node.key,
												initial: [800, 0],
												fixX: true,
												label: node.applicationName ,
												value: 1
											};
										}

									} else {
										var targetV = {
											category: 1,
											symbol: getImg(node.serviceTypeCode,node.applicationName),
											name: node.key,
											label: node.applicationName ,
											value: 1
										};

									}

									nodeMap[node.key] = targetV;
									targetK = node.key;
									linkText = node.totalCount;
								}
							});

							if(GetEscapeStr("exceptionFlag")) {
								linkData = {
									source: sourceK,
									target: targetK,
									weight: 2,
									itemStyle: {
										normal: {
											color:'#D3D3D3',

											text: link.totalCount,
											textColor: 	'#D3D3D3',
										
											textFont: 'bold 14px verdana',
											textPosition: 'inside',
											color: 'red'
										}
									}
								};
							} else {
								linkData = {
									source: sourceK,
									target: targetK,
									weight: 2,
									itemStyle: {
										normal: {
											color:'#D3D3D3',

											text: link.totalCount,
											textColor: 	'#D3D3D3',
										
											textFont: 'bold 14px verdana',
											textPosition: 'inside'
										}
									}
								};
							}

							linkArray.push(linkData);
						});

						//						nodeArray = ClearNullArr(nodeArray);
						for(var key in nodeMap) {
							nodeArray.push(nodeMap[key]);
						}

						var myChart = echarts.init(document.getElementById("rpc_link_chart"));
						var option = {

							tooltip: {
								trigger: 'item',
								formatter: '{a} : {b}'
							},
							toolbox: {
								show: true,
								feature: {
									restore: {
										show: true
									}
								}
							},
							series: [{
								type: 'force',
								name: "服务名称",
								ribbonType: false,

								itemStyle: {
									normal: {
										label: {
											show: true,
											position: 'bottom',
											textStyle: {
												color: 'black',
												baseline: 'left',
												fontStyle: 'normal', //文字字体的风格 'normal'标准 'italic'斜体 'oblique' 倾斜
												fontWeight: 'bolder', //'normal'标准'bold'粗的'bolder'更粗的'lighter'更细的或100 | 200 | 300 | 400...
												fontFamily: 'sans-serif', //文字的字体系列
												fontSize: 12 //字体大小
											}
										},
										nodeStyle: {
											brushType: 'both',
											borderColor: 'rgba(255,215,0,0.4)',
											borderWidth: 1
										}
									},
									emphasis: {
										label: {
											show: false
												// textStyle: null      // 默认使用全局文本样式，详见TEXTSTYLE
										},
										nodeStyle: {
											// r: 30

										},
										linkStyle: {}
									}
								},

								gravity: 4,
								scaling: 1.8,
								draggable: true,
								linkSymbol: 'arrow',
								symbolSize: 30,
								linkSymbolSize: [10, 22],
								steps: 10,
								coolDown: 0.9,
								//preventOverlap: true,
								nodes: nodeArray,

								links: linkArray
							}]
						};

						myChart.setOption(option);

						function focus(param) {
							var data = param.data;
							var links = option.series[0].links;
							var nodes = option.series[0].nodes;
							if(
								data.source != null &&
								data.target != null
							) { //点击的是边
								var sourceNode = nodes.filter(function(n) {
									return n.name == data.source
								})[0];
								var targetNode = nodes.filter(function(n) {
									return n.name == data.target
								})[0];
								console.log("选中了边 " + sourceNode.name + ' -> ' + targetNode.name + ' (' + data.weight + ')');
							} else { // 点击的是点
								console.log("选中了" + data.name + '(' + data.value + ')');
							}
						}

					});

			});
		</script>

	</body>

</html>